﻿

using System;
using System.Data;
using Model;
namespace Dal
{
	public class DbShippingCartDal
	{
		public DbShippingCartDal()
		{
		}
 		#region Insert
 		public static int Insert(DbShippingCart dbShippingCart)
		{	
			string sql = @"Insert Into DbShippingCart (
					UId,
					SessionId,
					PId,
					PName,
					PCode,
					PColor,
					PSize,
					PPic,
					PWeight,
					PPrice,
					Remarks,
					AddTime,
					Qty
				) values({0},'{1}',{2},'{3}','{4}','{5}','{6}','{7}',{8},{9},'{10}','{11}',{12})";
			sql = string.Format(sql,
					dbShippingCart.UId,
					SqlHelper.String(dbShippingCart.SessionId),
					dbShippingCart.PId,
					SqlHelper.String(dbShippingCart.PName),
					SqlHelper.String(dbShippingCart.PCode),
					SqlHelper.String(dbShippingCart.PColor),
					SqlHelper.String(dbShippingCart.PSize),
					SqlHelper.String(dbShippingCart.PPic),
					dbShippingCart.PWeight,
					dbShippingCart.PPrice,
					SqlHelper.String(dbShippingCart.Remarks),
					dbShippingCart.AddTime,
					dbShippingCart.Qty
				);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
		
		#region Update
 		public static int Update(DbShippingCart dbShippingCart)
		{	
			string sql = @"Update DbShippingCart set 

					UId= {0},
					SessionId= '{1}',
					PId= {2},
					PName= '{3}',
					PCode= '{4}',
					PColor= '{5}',
					PSize= '{6}',
					PPic= '{7}',
					PWeight= {8},
					PPrice= {9},
					Remarks= '{10}',
					AddTime= '{11}',
					Qty= {12}
				where

					CId= {13}";
			sql = string.Format(sql,
					dbShippingCart.UId,
					SqlHelper.String(dbShippingCart.SessionId),
					dbShippingCart.PId,
					SqlHelper.String(dbShippingCart.PName),
					SqlHelper.String(dbShippingCart.PCode),
					SqlHelper.String(dbShippingCart.PColor),
					SqlHelper.String(dbShippingCart.PSize),
					SqlHelper.String(dbShippingCart.PPic),
					dbShippingCart.PWeight,
					dbShippingCart.PPrice,
					SqlHelper.String(dbShippingCart.Remarks),
					dbShippingCart.AddTime,
					dbShippingCart.Qty,
					dbShippingCart.CId);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
 
 

 		#region Delete
 		public static int Delete(Int32 cId)
		{
		
			string sql = @"Delete from DbShippingCart where 
					CId= {0}";
			sql = string.Format(sql,
					cId
);
			return SqlHelper.ExecSql(sql);
		} 
		#endregion
 
 		#region Select
 		
 		public static DataSet Select()
		{
			string sql = "select a.* from DbShippingCart a "; 
			return SqlHelper.Select(sql);
		}
				
		public static DbShippingCart Select(Int32 cId)
		{
			DbShippingCart dbShippingCart = null;
			string sql = @"select a.* from DbShippingCart a  where
					a.CId= {0}
";
			sql = string.Format(sql,
					cId
);
			DataSet ds = SqlHelper.Select(sql);
			if(SqlHelper.HasRow(ds))
				dbShippingCart = new DbShippingCart(ds.Tables[0].Rows[0]);					
			return dbShippingCart;
		}
		
		public static DataSet Select(string where)
		{ 
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from DbShippingCart a  where 1=1 " + where ;
			return SqlHelper.Select(sql);
		}
		
		public static DataSet SelectForList(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;
						
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from dbShippingCart a where 1=1 " + where;
                
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}		
		public static DataSet Select(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;			
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;			
			string sql = "select a.* from DbShippingCart a  where 1=1 " + where;
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}
		#endregion
	}
}
